﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Supplier
{
    /// <summary>
    /// 根据id找对应的供应商信息
    /// </summary>
    public class SupplierSelIdHandler : IRequestHandler<SupplierSelIdCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Suppliers> supplierRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierRepository"></param>
        /// <param name="mapper"></param>
        public SupplierSelIdHandler(IBaseRepositoryGet<Suppliers> supplierRepository)
        {
            this.supplierRepository = supplierRepository;
        }

        public async Task<Result> Handle(SupplierSelIdCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            try
            {
                string sql = $"select * from Suppliers where SuppliersId={request.SuppliersId}";

                var list = supplierRepository.GetAll(sql, default);

                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "传值成功";
                }
            }
            catch (Exception)
            {

                throw;
            }

            return result;
        }
    }
}
